/* 对数值进行保留小数点后两位处理 */
const numFilter = (value, flag) => {
  // flag:true,对数值格式化处理  如 123,334,44
  let realVal = ''
  if (value === 0 || value) {
    // 截取当前数据到小数点后三位
    var a, tempVal
    if (String(value).indexOf('.') > -1) {
      a = parseFloat(value).toFixed(3)
      tempVal = a.substring(0, a.length - 1)
    } else {
      tempVal = String(value)
    }
    flag ? realVal = tempVal.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') : realVal = tempVal
  } else {
    realVal = '--'
  }
  return realVal
}
/* 求和echarts数值 */
// const sumE = (arr) => {
//   var sum = 0
//   arr.forEach(item => {
//     sum += item.value
//   })
//   return sum.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
// }
/* 对象值求和*/
// const sumObjValue = (obj) => {
//   var sum = 0
//   for (var key in obj) {
//     sum += obj[key]
//   }
//   return sum
// }

/* 根据yAxis的name是否存在返回对应的值 */
const yAxisName = (obj) => {
  return obj.name ? '(' + obj.name + ')' : ''
}

const getTable = (opt, colName, typeName) => {
  const axisData = opt.xAxis[0].data // 获取图形的data数组
  const axisLenth = opt.yAxis.length
  const series = opt.series // 获取series
  let num = 0 // 记录序号
  const sum = [] // 获取合计数组（根据对应的系数生成相应数量的sum）
  for (let i = 0; i < series.length; i++) {
    sum[i] = 0
  }
  let table = '<table class="charts-table"><thead><tr>' +
    '<th>' + colName + '</th>' +
    '<th>' + typeName + '</th>'
  for (let i = 0; i < series.length; i++) {
    if (axisLenth === 1) {
      table += '<th title=' + series[i].name + yAxisName(opt.yAxis[0]) + '>' + series[i].name + yAxisName(opt.yAxis[0]) + '</th>'
    } else {
      if (i === series.length - 1) {
        table += '<th title=' + series[i].name + yAxisName(opt.yAxis[1]) + '>' + series[i].name + yAxisName(opt.yAxis[1]) + '</th>'
      } else {
        table += '<th title=' + series[i].name + yAxisName(opt.yAxis[0]) + '>' + series[i].name + yAxisName(opt.yAxis[0]) + '</th>'
      }
    }
  }
  table += '</tr></thead><tbody>'
  for (let i = 0, l = axisData.length; i < l; i++) {
    num += 1
    for (let n = 0; n < series.length; n++) {
      if (series[n].data[i]) {
        sum[n] += Number(series[n].data[i])
      } else {
        sum[n] += Number(0)
      }
    }
    table += '<tr>' +
      '<td>' + num + '</td>' +
      '<td>' + axisData[i] + '</td>'
    for (let j = 0; j < series.length; j++) {
      if (series[j].data[i]) {
        table += '<td>' + numFilter(series[j].data[i], true) + '</td>'
      } else {
        table += '<td>' + 0 + '</td>'
      }
    }
    table += '</tr>'
  }
  if (opt.series0) {
    table += ''
  } else if (opt.series1) {
    // 最后一行加上次均费用平均值
    table += '<tr>' + '<td>' + (num + 1) + '</td>' + '<td>平均值</td>'
    table += '<td>' + numFilter((Number(opt.series1.cjfy)), true) + '</td>'
  } else if (opt.series2) {
    // 最后一行加上百分比平均值
    table += '<tr>' + '<td>' + (num + 1) + '</td>' + '<td>平均值</td>'
    table += '<td>' + numFilter((Number(opt.series2.zbxb)) * 100, true) + '</td>'
    table += '<td>' + numFilter((Number(opt.series2.sjb)) * 100, true) + '</td>'
    table += '<td>' + numFilter((Number(opt.series2.ejb)) * 100, true) + '</td>'
    table += '<td>' + numFilter((Number(opt.series2.yjb)) * 100, true) + '</td>'
    table += '<td>' + numFilter((Number(opt.series2.wjb)) * 100, true) + '</td>'
  } else if (opt.series3) {
    // 最后一行加上合计
    let sum0 = 0
    // let sum1 = 0
    // let sum2 = 0
    table += '<tr>' + '<td>' + (num + 1) + '</td>' + '<td>合计</td>'
    for (let n = 0; n < series.length - 1; n++) {
      table += '<td>' + numFilter(Number(sum[n]), true) + '</td>'
      sum0 = sum[0]
      // sum1 = sum[1]
      // sum2 = sum[2]
    }
    const Tpercent = numFilter(((sum[1] + sum[2]) / sum0) * 100, true)
    table += '<td>' + Tpercent + '</td>'
  } else {
    // 最后一行加上合计
    table += '<tr>' + '<td>' + (num + 1) + '</td>' + '<td>合计</td>'
    for (let n = 0; n < series.length; n++) {
      table += '<td>' + numFilter(Number(sum[n]), true) + '</td>'
    }
  }
  table += '</tr></tbody></table>'
  return table
}
const getTableY = (opt, colName, typeName) => {
  const axisData = opt.yAxis[0].data // 获取图形的data数组
  // const axisLenth = opt.xAxis.length
  const series = opt.series // 获取series
  let num = 0 // 记录序号
  const sum = [] // 获取合计数组（根据对应的系数生成相应数量的sum）
  for (let i = 0; i < series.length; i++) {
    sum[i] = 0
  }
  let table = '<table class="charts-table"><thead><tr>' +
    '<th>' + colName + '</th>' +
    '<th>' + typeName + '</th>'
  for (let i = 0; i < series.length; i++) {
    table += '<th>' + series[i].name + '</th>'
  }
  table += '</tr></thead><tbody>'
  for (let i = 0, l = axisData.length; i < l; i++) {
    num += 1
    for (let n = 0; n < series.length; n++) {
      if (series[n].data[i]) {
        if (series[n].data[i] instanceof Object) {
          sum[n] += Number(series[n].data[i].value)
        } else {
          sum[n] += Number(series[n].data[i])
        }
      } else {
        sum[n] += Number(0)
      }
    }
    table += '<tr><td>' + num + '</td>' +
      '<td>' + axisData[i] + '</td>'
    for (let j = 0; j < series.length; j++) {
      if (series[j].data[i]) {
        if (series[j].data[i] instanceof Object) {
          table += '<td>' + numFilter(series[j].data[i].value, true) + '</td>'
        } else {
          table += '<td>' + numFilter(series[j].data[i], true) + '</td>'
        }
      } else {
        table += '<td>' + 0 + '</td>'
      }
    }
    table += '</tr>'
  }
  table += '<tr><td>' + (num + 1) + '</td>' + '<td>合计</td>'
  for (let n = 0; n < series.length; n++) {
    table += '<td>' + numFilter(Number(sum[n]), true) + '</td>'
  }
  table += '</tr></tbody></table>'
  return table
}
/**
 *
 * @param opt 图表option
 * @param colName
 * @param typeName X或Y轴 表头名
 * @returns {string}
 */
const getTablePie = (opt, colName, typeName) => {
  const series = opt.series[0].data // 获取series
  let num = 0 // 记录序号
  let table = '<table class="charts-table"><thead><tr>' +
    '<th>' + colName + '</th>' +
    '<th>' + opt.series[0].name + '</th>' +
    '<th>' + typeName + '</th></tr></thead><tbody>'
  // for (let i = 0; i < series.length; i++) {
  //   table += "<th title=" + series[i].name + ">" + series[i].name + '</th>'
  // }
  // table += '</tr></thead><tbody><tr>'
  for (let i = 0; i < series.length; i++) {
    num += 1
    table += '<tr><td>' + num + '</td><td>' + series[i].name + '</td><td>' + series[i].value + '</td></tr>'
  }
  // <td>' + (series[i].value).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</td>
  // table += '<td>' + sum.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</td>'
  table += '</tbody></table>'
  return table
}
const getTableCircle = (opt, colName, typeName) => {
  const series1 = opt.series[0].data // 获取第一个series
  const series2 = opt.series[1].data // 获取第一个series
  const series = Object.assign(series1, series2) // 拼接两个对象
  let num = 0 // 记录序号
  let table = '<table class="charts-table"><thead><tr>' +
  '<th>' + colName + '</th>' +
  '<th>' + opt.series[0].name + '</th>' +
  '<th>' + typeName + '</th></tr></thead><tbody>'
  for (let i = 0; i < series.length; i++) {
    num += 1
    table += '<tr><td>' + num + '</td><td>' + series[i][0] + '</td><td>' + series[i][1] + '</td></tr>'
  }
  // <td>' + (series[i].value).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</td>
  // table += '<td>' + sum.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</td>'
  table += '</tbody></table>'
  return table
}
/**
 *
 * @param typeName  X或Y轴表头名
 * @returns {{orient: string, feature: {dataView: {backgroundColor: string, buttonColor: string, show: boolean, contentToOption: feature.dataView.contentToOption, readOnly: boolean, buttonTextColor: string, title: string, lang: string[], optionToContent: (function(*=): string), textColor: string}}, showTitle: boolean, show: boolean}}
 */
export function toolbox(typeName) {
  return {
    show: true,
    x: '95%',
    orient: 'horizontal',
    showTitle: true,
    feature: {
      dataView: {
        show: true,
        title: '数据视图',
        lang: ['<div style="font-size:14px">数据视图</div>', '<div class="tabBtn">关闭</div>', '<div class="tabBtn">导出excel</div>'],
        textColor: '#000',
        readOnly: false,
        backgroundColor: 'rgba(255, 255, 255,1)',
        buttonTextColor: '#fff',
        buttonColor: '#005FD3',
        optionToContent: function(opt) {
          const colName = '序号'
          const table = getTable(opt, colName, typeName || '')
          return table
        },
        contentToOption: function(opt) {
          // 数据视图导出的方法
          const tableStr = opt.innerHTML
          const begin = tableStr.indexOf('<table')
          const last = tableStr.length
          let tableHtml = '<html><head><meta charset="UTF-8"></head><body>' + tableStr.substring(begin, last)
          tableHtml += '</body></html>'
          const excelBlob = new Blob([tableHtml], {
            type: 'application/vnd.ms-excel'
          })
          const oa = document.createElement('a')
          oa.href = URL.createObjectURL(excelBlob)
          oa.download = 'table.xls'
          document.body.appendChild(oa)
          oa.click()
        }
      }
    }
  }
}

/**
 *
 * @param typeName X或Y轴的表头名
 * @returns {{orient: string, feature: {dataView: {backgroundColor: string, buttonColor: string, show: boolean, contentToOption: feature.dataView.contentToOption, readOnly: boolean, buttonTextColor: string, title: string, lang: string[], optionToContent: (function(*=): string), textColor: string}}, showTitle: boolean, show: boolean, right: string}}
 */
export function toolboxY(typeName) {
  return {
    show: true,
    // x: 'left',
    orient: 'horizontal',
    showTitle: true,
    right: '10',
    feature: {
      dataView: {
        show: true,
        title: '数据视图',
        lang: ['<div style="font-size:14px">数据视图</div>', '<div class="tabBtn">关闭</div>', '<div class="tabBtn">导出excel</div>'],
        textColor: '#000',
        readOnly: false,
        backgroundColor: 'rgba(255, 255, 255,1)',
        buttonTextColor: '#fff',
        buttonColor: '#005FD3',
        optionToContent: function(opt) {
          const colName = '序号'
          const table = getTableY(opt, colName, typeName || '')
          return table
        },
        contentToOption: function(opt) {
          // 数据视图导出的方法
          const tableStr = opt.innerHTML
          const begin = tableStr.indexOf('<table')
          const last = tableStr.length
          let tableHtml = '<html><head><meta charset="UTF-8"></head><body>' + tableStr.substring(begin, last)
          tableHtml += '</body></html>'
          const excelBlob = new Blob([tableHtml], {
            type: 'application/vnd.ms-excel'
          })
          const oa = document.createElement('a')
          oa.href = URL.createObjectURL(excelBlob)
          oa.download = 'table.xls'
          document.body.appendChild(oa)
          oa.click()
        }
      }
    }
  }
}
export function toolboxPie(typeName) {
  return {
    show: true,
    orient: 'horizontal',
    showTitle: true,
    top: '20',
    right: '30',
    feature: {
      dataView: {
        show: true,
        title: '数据视图',
        textColor: '#000',
        lang: ['<div style="font-size:14px">数据视图</div>', '<div class="tabBtn">关闭</div>', '<div class="tabBtn">导出excel</div>'],
        backgroundColor: 'rgba(255, 255, 255,1)',
        buttonTextColor: '#fff',
        buttonColor: '#005FD3',
        optionToContent: function(opt) {
          const colName = '序号'
          const table = getTablePie(opt, colName, typeName || '')
          return table
        },
        contentToOption: function(opt) {
          const tableStr = opt.innerHTML
          const begin = tableStr.indexOf('<table')
          const last = tableStr.length
          let tableHtml = '<html><head><meta charset="UTF-8"></head><body>' + tableStr.substring(begin, last)
          tableHtml += '</body></html>'
          const excelBlob = new Blob([tableHtml], {
            type: 'application/vnd.ms-excel'
          })
          const oa = document.createElement('a')
          oa.href = URL.createObjectURL(excelBlob)
          oa.download = 'table.xls'
          document.body.appendChild(oa)
          oa.click()
        }
      }
    }
  }
}
export function toolboxCircle(typeName) {
  return {
    show: true,
    orient: 'horizontal',
    showTitle: true,
    top: '20',
    right: '30',
    feature: {
      dataView: {
        show: true,
        title: '数据视图',
        textColor: '#000',
        lang: ['<div style="font-size:14px">数据视图</div>', '<div class="tabBtn">关闭</div>', '<div class="tabBtn">导出excel</div>'],
        backgroundColor: 'rgba(255, 255, 255,1)',
        buttonTextColor: '#fff',
        buttonColor: '#005FD3',
        optionToContent: function(opt) {
          const colName = '序号'
          const table = getTableCircle(opt, colName, typeName || '')
          return table
        },
        contentToOption: function(opt) {
          const tableStr = opt.innerHTML
          const begin = tableStr.indexOf('<table')
          const last = tableStr.length
          let tableHtml = '<html><head><meta charset="UTF-8"></head><body>' + tableStr.substring(begin, last)
          tableHtml += '</body></html>'
          const excelBlob = new Blob([tableHtml], {
            type: 'application/vnd.ms-excel'
          })
          const oa = document.createElement('a')
          oa.href = URL.createObjectURL(excelBlob)
          oa.download = 'table.xls'
          document.body.appendChild(oa)
          oa.click()
        }
      }
    }
  }
}

